// pages/mine/mine.js
const app = getApp()
const util = require('../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: null,
    department: null,
    identity: null,
    phone: null,
    visitor: false,
    student: false,
    loaded: false,
    serviceURL: app.imageUrl
  },


  onShow: function(options) {
    // wx.request({
    //   url: app.baseUrl + '/AccountManagement/getUserByWeChatId',
    //   method: 'POST',
    //   data: {
    //     weChatId: app.globalData.wechatId
    //   },
    //   dataType: 'json',
    //   success: (res) => {
    //     if (res.data != []) {
    // this.setData({
    //   name: res.data[0].realName,
    //   department: res.data[0].department,
    //   identity: res.data[0].identity,
    //   phone: res.data[0].phone
    // })
    //     }
    //   }
    // })
    console.log(app.globalData)
    this.setData({
      name: app.globalData.realname,
      department: app.globalData.department,
      identity: app.globalData.identity,
      phone: app.globalData.phone,
      userId: app.globalData.userId,
      identityId: app.globalData.identityId,
      departmentId: app.globalData.departmentId,
    })
    if (["-1", "-2"].indexOf(app.globalData.identityId) != -1) {
      this.setData({
        visitor: true
      })
      if (app.globalData.identityId == '-1') {
        this.setData({
          student: true
        })
      }
    }
    this.setData({
      loaded: true
    })
  },

  logout: util.throttle(function() {
    wx.showModal({
      title: "",
      content: '是否确认注销当前账号？点击确定后将返回到登录界面',
      success(res) {
        if (!res.confirm) {
          return
        }
        wx.showLoading({
          title: '注销中',
          mask: true
        })
        wx.request({
          url: app.userUrl + '/WeChat/AccountManagement/deleteUserByWeChatId',
          method: 'POST',
          data: app.globalData.wechatId,
          dataType: 'json',
          success: (res) => {
            if (res.data == "fail") {
              wx.showToast({
                title: '请重试',
                mask: true
              })
              return
            }
            wx.showToast({
              title: '注销成功',
            })
            app.globalData.isSupervising = false
            app.globalData.superviseSchoolId = 0
            app.globalData.superviseSchoolName = ""
            app.globalData.superviseId = 0
            app.globalData.isSuddenSupervising = false
            app.globalData.suddensuperviseSchoolId = 0
            app.globalData.suddensuperviseSchoolName = ""
            app.globalData.suddensuperviseId = 0
            wx.reLaunch({
              url: '/pages/login/login'
            })
          },
          fail: res => {
            wx.showToast({
              title: '网络异常！',
              image: '/images/warning.png'
            })
          }
        })
      }
    })
  }, 800),

  qrCode: util.throttle(function() {
    // if ("8" == app.globalData.identityId) {
    if (app.globalData.features.indexOf("35") != -1) {
      wx.navigateTo({
        url: '/pages/signInIndex/signInIndex?fromSta=1',
      })
    } else {
      wx.navigateTo({
        url: '/pages/signInIndex/signInIndex',
      })
      // util.scanQRCode()
    }
  }),

  pushCode: util.throttle(function () {
      wx.navigateTo({
        url: '/pages/pushIntroduce/pushIntroduce',
      })
  }),

  toWorkRecord:util.throttle(function(){
    wx.request({
      url: app.backstageUrl + '/WeChat/Statistics/isDutyResponsibilityPerson',
      method: 'GET',
      data: {
        departmentId : app.globalData.departmentId ,
        //app.globalData.realname
        //'张战恩' app.globalData.realname
        realname : app.globalData.realname
      },
      dataType: 'json',
      success: (res) => {
        console.log("data",res.data)
        if(res.data.dutyFlag == 1){
          wx.navigateTo({
            url: "/subpages/pages/allworkHistory/allworkHistory"
          })
        }else if(res.data.dutyFlag ==0){
          wx.navigateTo({
            url: "/pages/individualworkhistory/individualworkhistory"
          })
        }
      },
    })
  }),

  openSetting() {
    wx.openSetting()
  }

})